﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ThanhTu.GNCMedia.KQXS.DataAccess.DO;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using ThanhTu.GNCMedia.KQXS.Common;

namespace ThanhTu.GNCMedia.KQXS.DataAccess.DAL
{
    public class TimeofAwardsDAL : BaseDAL
    {
        public TimeofAwardsDAL() : base() { }


        public int SaveTimeAward(TimeofAwardsDO vTimeofAwardsDO)
        {
            OracleCommand oraComm = new OracleCommand();
            oraComm.CommandType = CommandType.StoredProcedure;
            oraComm.CommandText = "TimeAward_PKG.SaveTimeAward";
            OracleParameter oraParam = null;

            oraParam = new OracleParameter("IN_DateAward", OracleDbType.Varchar2);
            oraParam.Value = vTimeofAwardsDO.DateOfAwards;
            oraComm.Parameters.Add(oraParam);

            oraParam = new OracleParameter("IN_Province_ID", OracleDbType.Varchar2);
            oraParam.Value = vTimeofAwardsDO.ProvinceID;
            oraComm.Parameters.Add(oraParam);


            oraParam = new OracleParameter("OUTRETURN", OracleDbType.Int32);
            oraParam.Direction = ParameterDirection.Output;
            oraComm.Parameters.Add(oraParam);

            int result = ExecuteNoneQuery(oraComm);

            if (!Convert.IsDBNull(oraComm.Parameters["OUTRETURN"]))
            { result = Convert.ToInt32(oraComm.Parameters["OUTRETURN"].Value.ToString()); }

            oraComm.Dispose();
            oraComm = null;
            oraParam = null;
            return result;
        }
        public int delete(TimeofAwardsDO vTimeofAwardsDO)
        {
            OracleCommand oraComm = new OracleCommand();
            oraComm.CommandType = CommandType.StoredProcedure;
            oraComm.CommandText = "TimeAward_PKG.DeleteTimeAward";
            OracleParameter oraParam = null;

            oraParam = new OracleParameter("IN_DateAward", OracleDbType.Varchar2);
            oraParam.Value = vTimeofAwardsDO.DateOfAwards;
            oraComm.Parameters.Add(oraParam);

            oraParam = new OracleParameter("IN_Province_ID", OracleDbType.Varchar2);
            oraParam.Value = vTimeofAwardsDO.ProvinceID;
            oraComm.Parameters.Add(oraParam);


            oraParam = new OracleParameter("OUTRETURN", OracleDbType.Int32);
            oraParam.Direction = ParameterDirection.Output;
            oraComm.Parameters.Add(oraParam);

            int result = ExecuteNoneQuery(oraComm);

            if (!Convert.IsDBNull(oraComm.Parameters["OUTRETURN"]))
            { result = Convert.ToInt32(oraComm.Parameters["OUTRETURN"].Value.ToString()); }

            oraComm.Dispose();
            oraComm = null;
            oraParam = null;
            return result;
        }

        public List<TimeofAwardsDO> SelectALL_QT()
        {
            OracleCommand oraComm = new OracleCommand();
            oraComm.CommandType = CommandType.StoredProcedure;
            oraComm.CommandText = "TIMEAWARD_PKG.GetAllTimeAward_admin";
            OracleParameter oraParam = null;

            oraParam = new OracleParameter("OUTRETURN", OracleDbType.RefCursor);
            oraParam.Direction = ParameterDirection.Output;
            oraComm.Parameters.Add(oraParam);

            DataSet ds = GetDataSet(oraComm);
            DataTable dt = null;
            List<TimeofAwardsDO> vTimeofAwardsDOCollection = null;
            DataRow dr = null;
            TimeofAwardsDO vTimeofAwardsDO = null;

            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    dt = ds.Tables[0];
                    vTimeofAwardsDOCollection = new List<TimeofAwardsDO>();

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        dr = dt.Rows[i];
                        vTimeofAwardsDO = new TimeofAwardsDO();

                        if (!Convert.IsDBNull(dr["PROVINCEID"]))
                        { vTimeofAwardsDO.ProvinceID = Convert.ToString(dr["PROVINCEID"]); }

                        if (!Convert.IsDBNull(dr["DATEAWARDS"]))
                        { vTimeofAwardsDO.DateOfAwards = Utils.ToUTF8String((byte[])dr["DATEAWARDS"]);  }

                        vTimeofAwardsDOCollection.Add(vTimeofAwardsDO);
                        dr = null;
                        vTimeofAwardsDO = null;
                    }

                    dt.Dispose();
                    dt = null;
                }

                ds.Dispose();
                ds = null;
            }
            oraComm.Dispose();
            oraComm = null;
            oraParam = null;

            return vTimeofAwardsDOCollection;
        }

        public List<TimeofAwardsDO> SelectALL_Date_QT(string vDate_Wards)
        {
            OracleCommand oraComm = new OracleCommand();
            oraComm.CommandType = CommandType.StoredProcedure;
            oraComm.CommandText = "TIMEAWARD_PKG.GetAllTimeAward_Date";
            OracleParameter oraParam = null;

            oraParam = new OracleParameter("IN_DateAward", OracleDbType.Varchar2);
            oraParam.Value = vDate_Wards;
            oraComm.Parameters.Add(oraParam);

            oraParam = new OracleParameter("OUTRETURN", OracleDbType.RefCursor);
            oraParam.Direction = ParameterDirection.Output;
            oraComm.Parameters.Add(oraParam);

            DataSet ds = GetDataSet(oraComm);
            DataTable dt = null;
            List<TimeofAwardsDO> vTimeofAwardsDOCollection = null;
            DataRow dr = null;
            TimeofAwardsDO vTimeofAwardsDO = null;

            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    dt = ds.Tables[0];
                    vTimeofAwardsDOCollection = new List<TimeofAwardsDO>();

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        dr = dt.Rows[i];
                        vTimeofAwardsDO = new TimeofAwardsDO();

                        if (!Convert.IsDBNull(dr["PROVINCEID"]))
                        { vTimeofAwardsDO.ProvinceID = Convert.ToString(dr["PROVINCEID"]); }

                        if (!Convert.IsDBNull(dr["DATEAWARDS"]))
                        { vTimeofAwardsDO.DateOfAwards =  Utils.ToUTF8String((byte[])dr["DATEAWARDS"]) ; }

                        vTimeofAwardsDOCollection.Add(vTimeofAwardsDO);
                        dr = null;
                        vTimeofAwardsDO = null;
                    }

                    dt.Dispose();
                    dt = null;
                }

                ds.Dispose();
                ds = null;
            }
            oraComm.Dispose();
            oraComm = null;
            oraParam = null;

            return vTimeofAwardsDOCollection;
        }

        public DataTable SelectAll_TimeAward_user(int pageIndex, int pageSize, ref int rowCount)
        {
            OracleCommand oraComm = new OracleCommand();
            oraComm.CommandType = CommandType.StoredProcedure;
            oraComm.CommandText = "TIMEAWARD_PKG.GetAllTimeAward_user";
            OracleParameter oraParam = null;

            oraParam = new OracleParameter("INPAGEINDEX", OracleDbType.Int32);
            oraParam.Value = pageIndex;
            oraComm.Parameters.Add(oraParam);

            oraParam = new OracleParameter("INPAGESIZE", OracleDbType.Int32);
            oraParam.Value = pageSize;
            oraComm.Parameters.Add(oraParam);

            oraParam = new OracleParameter("OUTRETURN", OracleDbType.RefCursor);
            oraParam.Direction = ParameterDirection.Output;
            oraComm.Parameters.Add(oraParam);

            oraParam = new OracleParameter("OUTROWCOUNT", OracleDbType.Int32);
            oraParam.Direction = ParameterDirection.Output;
            oraComm.Parameters.Add(oraParam);

            DataSet ds = GetDataSet(oraComm);
            DataTable dt = null;

            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    dt = ds.Tables[0];

                    if (!Convert.IsDBNull(oraComm.Parameters["OUTROWCOUNT"]))
                    { rowCount = Convert.ToInt32(oraComm.Parameters["OUTROWCOUNT"].Value.ToString()); }
                }

                ds.Dispose();
                ds = null;
            }

            oraComm.Dispose();
            oraComm = null;
            oraParam = null;

            return dt;
        }
    }
}
